/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.adapters;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.domain.user.entity.UserRolesE;
import com.itgrail.pilot.domain.user.ports.UserRolePort;
import com.itgrail.pilot.domain.user.valueobject.UserRolesVO;
import com.itgrail.pilot.infrastructure.converter.UserRolesConverter;
import com.itgrail.pilot.infrastructure.tunnel.db.dao.UserRolesMapper;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserRolesDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.itgrail.grail.ddd.annotation.infrastructure.Adapter;
import com.itgrail.grail.utils.reflect.BeanMapper;

import java.util.List;


@Adapter
public class UserRolesAdapters implements UserRolePort {


    @Autowired
    private UserRolesMapper userRolesMapper;

    @Autowired
    private UserRolesConverter userRolesConverter;

    @Override
    public void createUserRole(String userName, String role) {
        QueryWrapper<UserRolesDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userName);
        queryWrapper.eq("is_deleted", OkrConstants.NOT_DELETED);
        UserRolesDO userRolesDO = userRolesMapper.selectOne(queryWrapper);
        if (userRolesDO == null) {
            userRolesDO = new UserRolesDO();
            userRolesDO.setUsername(userName).setRole(role);
            userRolesMapper.insert(userRolesDO);
        }
    }

    @Override
    public void saveOrUpdate(UserRolesVO userRolesVO) {
        String username = userRolesVO.getUsername();
        String role = userRolesVO.getRole();
        if(role.equals("普通用户")){
            role="USER";
        }
        if(role.equals("管理员")){
            role="ADMIN";
        }
        if(role.equals("黑名单")){
            role="BLACK";
        }
        if (StringUtils.isNotEmpty(username)) {
            UserRolesDO userRolesDO = userRolesMapper.queryByUserName(username);
            if (userRolesDO != null) {
                userRolesDO.setRole(role);
                userRolesMapper.updateById((userRolesDO));
            } else {
                userRolesMapper.insert(BeanMapper.map(userRolesVO, UserRolesDO.class));
            }
        }
    }

    @Override
    public List<UserRolesE> queryByRole(String role) {
        List<UserRolesDO> userRolesDOS = userRolesMapper.queryByRole(role);
        List<UserRolesE> userRolesE = userRolesConverter.dataToEntities(userRolesDOS);
        return userRolesE;
    }

    @Override
    public boolean isAdmin(String userName) {
        if (StringUtils.isEmpty(userName)) {
            return false;
        }
        List<String> userRoles = userRolesMapper.findUserRoles(userName);
        return userRoles.contains("ADMIN");
    }

    @Override
    public void delete(String account) {
        QueryWrapper<UserRolesDO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", account);
        queryWrapper.eq("is_deleted", 0);
        UserRolesDO userRolesDO = userRolesMapper.selectOne(queryWrapper);
        if (null != userRolesDO) {
            userRolesMapper.deleteById(userRolesDO.getId());
        }
    }
}
